<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      Array.prototype.myReduce = function (callback, init) {
        //判断是否有初始值,没有第一项就是初始值
        let result = init ? init : this[0]
        for (let i = init ? 0 : 1; i < this.length; i++) {
          //返回每次的result+this[i]的值，对result进行不断更新
          result = callback(result, this[i], i, this)
        }
        return result
      }

      let arr = [12, 52, -3, 12, 6]
      let res = arr.myReduce((prev, item) => {
        return prev + item
      }, 0)
      console.log(res)
    </script>
  </body>
</html>
